package com.zs.games.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.games.resultTool.ResultCodeEnum;
import com.zs.games.entity.*;
import com.zs.games.enums.SourceTypeEnum;
import com.zs.games.exception.APIException;
import com.zs.games.mapper.*;
import com.zs.games.param.PageQuery;
import com.zs.games.param.UserCommentGameParam;
import com.zs.games.service.GameInfoService;
import com.zs.games.utils.DateUtil;
import com.zs.games.utils.JwtTokenUtils;
import com.zs.games.vo.GameInfoVo;
import com.zs.games.vo.LoginUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GameInfoServiceImpl implements GameInfoService {

    @Autowired
    private GameInfoMapper gameInfoMapper;
    @Autowired
    private GameLiveMapper gameLiveMapper;
    @Autowired
    private UserCommentMapper userCommentMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public IPage<GameInfoVo> getPageList(PageQuery pageQuery) {
        LoginUserVo userInfo = JwtTokenUtils.getUserInfo();
        IPage<GameInfoVo> gameInfoPage = gameInfoMapper.findGameInfoPage(new Page<GameInfoVo>(pageQuery.getPageNumber(), pageQuery.getPageSize()), userInfo == null ? null : userInfo.getUserId());
        for (GameInfoVo record : gameInfoPage.getRecords()) {
            getGameInfoVo(record);
        }
        return gameInfoPage;
    }

    private GameInfoVo getGameInfoVo(GameInfoVo gameInfo){
        UserInfo userInfo = userInfoMapper.selectById(gameInfo.getCreateUserId());
        if (userInfo != null) {
            gameInfo.setCreateUserNickName(userInfo.getNickName());
        }
        gameInfo.setUserGameCommentList(userCommentMapper.getUserCommentList(gameInfo.getGameId(), SourceTypeEnum.GAME.getValue()));
        Integer liveNum = gameLiveMapper.selectCount(new QueryWrapper<GameLive>().lambda().eq(GameLive::getGameId, gameInfo.getGameId()).eq(GameLive::getLiveStatus, 1));
        Integer noLive = gameLiveMapper.selectCount(new QueryWrapper<GameLive>().lambda().eq(GameLive::getGameId, gameInfo.getGameId()).eq(GameLive::getLiveStatus, 2));
        gameInfo.setLiveNum(liveNum);
        gameInfo.setNoLiveNum(noLive);
        gameInfo.setDateStr(DateUtil.getDateStr(gameInfo.getCreateTime(), DateUtil.FORMAT_YYYY_MM_DD));
        LoginUserVo userInfo1 = JwtTokenUtils.getUserInfo();
        if (userInfo1 != null){
            List<GameLive> gameLives = gameLiveMapper.selectList(new QueryWrapper<GameLive>().lambda().eq(GameLive::getGameId, gameInfo.getGameId())
                    .eq(GameLive::getUserId, userInfo1.getUserId()));
            if (!gameLives.isEmpty()){
                gameInfo.setMyLiveStatus(gameLives.get(0).getLiveStatus());
            }
        }
        return gameInfo;
    }

    @Override
    public GameInfoVo getGameInfoById(Integer gameId) {
        GameInfo gameInfo = gameInfoMapper.selectById(gameId);
        if (gameInfo == null) {
            throw new APIException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        GameInfoVo gameInfoVo = new GameInfoVo();
        BeanUtils.copyProperties(gameInfo, gameInfoVo);
        return getGameInfoVo(gameInfoVo);
    }

    @Override
    public boolean addGameInfo(GameInfo gameInfo) {
        gameInfo.setCreateTime(new Date());
        gameInfo.setCreateUserId(JwtTokenUtils.getUserInfoA().getUserId());
        int rows = gameInfoMapper.insert(gameInfo);
        return rows > 0;
    }

    @Override
    public boolean updateGameInfo(GameInfo gameInfo) {
        int rows = gameInfoMapper.updateById(gameInfo);
        return rows > 0;
    }

    @Override
    public boolean deleteGameInfo(Integer gameId) {
        int rows = gameInfoMapper.deleteById(gameId);
        return rows > 0;
    }

    @Override
    public boolean liveGameInfo(Integer gameId, Integer liveStatus) {
        GameInfo gameInfoById = getGameInfoById(gameId);
        if (gameInfoById == null){
            throw new APIException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        Integer count = gameLiveMapper.selectCount(new QueryWrapper<GameLive>().lambda().eq(GameLive::getGameId, gameId).eq(GameLive::getUserId, JwtTokenUtils.getUserInfoA().getUserId()));
        if (count > 0){
            throw new APIException(ResultCodeEnum.ALREADY_EXIST);
        }
        return gameLiveMapper.insert(new GameLive(gameId, JwtTokenUtils.getUserInfoA().getUserId(), liveStatus)) > 0;

    }

    @Override
    public boolean commentGame(UserCommentGameParam param) {
        UserComment userComment = new UserComment(null,param.getGameId(), JwtTokenUtils.getUserInfoA().getUserId(), param.getContent(),SourceTypeEnum.GAME);
        return userCommentMapper.insert(userComment) > 0;
    }

    @Override
    public boolean deleteGameComment(Integer commentId) {
        UserComment userGameComment = userCommentMapper.selectById(commentId);
        if (userGameComment == null){
            throw new APIException(ResultCodeEnum.DATA_NOT_EXIST);
        }
        if (!userGameComment.getUserId().equals(JwtTokenUtils.getUserInfoA().getUserId())){
            throw new APIException(ResultCodeEnum.COMMENT_DELETE_ERROR);
        }
        return userCommentMapper.deleteById(commentId) > 0;
    }

}
